<template>
  <div class="attachment-item-wrap">
    <div v-if="isLatest" class="attachment-header">本合同最新附件</div>
    <div class="attachement-item-content">
      <div class="attachment-item-title">
        <UserCard :user-data="{ id: item.userId, username: item.username, avatar: item.avatar }" :size="32" class="mr10" />
        <div class="attachment-item-name">{{ item.username }}</div>
        <div :class="sourceClass" class="attachment-item-source">{{ item.source | sourceStatus }}</div>
        <div class="attachment-item-time">{{ item.time | format('YYYY/MM/DD HH:mm') }}</div>
        <el-button v-if="userInfo.id === item.userId || hasRouteAuth('crm:contract:attachment:delete')" style="margin: 0 12px;" type="text" @click.stop="handleDelete(item.id)">删除</el-button>
      </div>
      <div class="attachment-item-list">
        <div class="attachment-list">
          <div v-for="(it, idx) in item.items" :key="it.uid" class="attachment-list-item">
            <div class="list-left">
              <span v-if="!idx">附件：</span>
            </div>
            <div class="attachment-url">
              <i class="el-icon-paperclip" />
              <ToolTip :content="it.name" style="color: #666666;" />
            </div>
            <div class="attachment-btn">
              <el-button type="text" @click.stop="handleDownload(it.url, it.name)">下载</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import UserCard from '@/components/Common/userCard';
import { format } from '@/filters/date';
import { sourceStatus } from '@/filters/status';
import { mapState } from 'vuex';
import { downloadFile } from '@/utils';
import { deleteAttachment, attachmentDownload } from '@/api/legal';
import { hasRouteAuth } from '@/utils/permission';
export default {
  components: { UserCard },
  filters: { format, sourceStatus },
  props: {
    item: {
      type: Object,
      default: null
    },
    isLatest: {
      type: Boolean,
      default: false
    },
    contractId: {
      type: Number,
      default: null
    }
  },
  computed: {
    ...mapState({
      'userInfo': state => state.user.userInfo
    }),
    sourceClass() {
      const map = ['contract', 'audit', 'comment'];
      return map[this.item.source];
    }
  },
  methods: {
    hasRouteAuth,
    handleDownload(url, name) {
      downloadFile(url, name);
      const { id, businessId } = this.item;
      const data = {
        id,
        businessId,
        name
      };
      attachmentDownload(data).then(res => {
        if (res.code === 200) {
          this.$emit('refresh');
        }
      });
    },
    handleDelete(id) {
      this.$confirm('请确认是否删除附件', '', {
        confirmButtonText: '确定',
        type: 'warning',
        showClose: false,
        center: true,
        dangerouslyUseHTMLString: true,
        iconClass: 'charge-icon',
        customClass: 'del-message del-message-cancel'
      }).then(() => {
        deleteAttachment(id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功');
            this.$emit('deleteAttachment');
          }
        }).catch(err => console.error(err.message));
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.attachment-item-wrap {
  background: #FFFFFF;
  box-shadow: 0px 3px 6px #DDE1EA;
  opacity: 1;
  border-radius: 8px;
  .attachment-header {
    color: #fff;
    background-color: #406EFF;
    border-radius: 8px 8px 0 0;
    padding: 5px 12px 5px;
  }
  .attachement-item-content {
    padding: 12px;
    .attachment-item-title {
      display: flex;
      align-items: center;
      .attachment-item-name {
        margin-right: 10px;
      }
      .attachment-item-source {
        border-radius: 10px;
        font-size: 12px;
        width: 56px;
        height: 20px;
        line-height: 20px;
        padding: 0 5px;
        text-align: center;
        white-space: nowrap;
        &.contract {
          color: #31CC9C;
          background-color: #E2F8F2;
        }
        &.audit {
          color: #FD8546;
          background-color: #FFF2EB;
        }
        &.comment {
          color: #406EFF;
          background-color: #EFF4FF;
        }
      }
      .attachment-item-time {
        flex: 1;
        color: #999999;
        text-align: right;
      }
    }
    .attachment-item-list {
      padding: 1px 12px;
      background-color: #FAFAFC;
      width: 100%;
      .list-left {
        color: #333333;
        line-height: 38px;
        font-weight: bold;
        width: 42px;
        min-width: 42px;
      }
      .attachment-list {
        width: 100%;
        .attachment-list-item {
          display: flex;
          justify-content: space-between;
          .attachment-url {
            flex: 1;
            display: flex;
            align-items: center;
            min-width: 0;
            &::v-deep {
              .tooltip-container {
                max-width: calc(100% - 20px);
              }
            }
          }
          .attachment-btn {
            display: flex;
          }
        }
      }
    }
  }
}
</style>
